package 并发编程07.ForkJoin分区域打印;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 作者：idea
 * 日期：2018/8/19
 * 描述：
 */
public class TaskPrint extends RecursiveTask<String> {

    private int taskCount=4;
    private static int currentIndex=1;
    private String result="hahah";
    private String item;
    private static List<TaskPrint> taskPrintsList=new ArrayList<>();
    public TaskPrint(String result) {
        this.item = result;
        taskCount--;
    }


    @Override
    protected String compute() {
        if(taskCount==1){
            return result;
        }else{
            TaskPrint p1=new TaskPrint("idea1---"+taskCount);
            p1.fork();
            String newContent=p1.join();
            result=result+newContent;
            return result;
        }
    }

    public static void main(String[] args) {
//        TaskPrint p1=new TaskPrint("idea1");
//        p1.fork();
//        TaskPrint p2=new TaskPrint("idea2");
//        p2.fork();
//
//        String result=p1.join();
//        result+=p2.join();
//        System.out.println(result);


        ForkJoinPool forkJoinPool=new ForkJoinPool();
        TaskPrint tp=new TaskPrint("0");
        Future<String> future=forkJoinPool.submit(tp);
        System.out.println("字符串开始拼接");
        try {
            System.out.println(future.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}
